﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using personalplaner.common.mvvm.mediator;
using System.Threading;
using personalplaner.common.threading;
using personalplaner.databasewizard.viewmodels;
using System.Windows.Threading;

namespace personalplaner.databasewizard.views
{
    /// <summary>
    /// Interaction logic for StatusView.xaml
    /// </summary>
    public partial class StatusView : UserControl
    {
        public StatusView()
        {
            InitializeComponent();

            //statusListWrapper.Child = CreateMediaElementOnWorkerThread();
        }

        //private static AutoResetEvent s_event = new AutoResetEvent(false);
        //ListBox waitIndicator;

        //private HostVisual CreateMediaElementOnWorkerThread()
        //{
        //    // Create the HostVisual that will "contain" the VisualTarget
        //    // on the worker thread.
        //    HostVisual hostVisual = new HostVisual();

        //    // Spin up a worker thread, and pass it the HostVisual that it
        //    // should be part of.
        //    Thread thread = new Thread(new ParameterizedThreadStart(MediaWorkerThread));
        //    thread.SetApartmentState(ApartmentState.STA);
        //    thread.IsBackground = true;
        //    thread.Start(hostVisual);

        //    // Wait for the worker thread to spin up and create the VisualTarget.
        //    s_event.WaitOne();

        //    return hostVisual;
        //}

        //private void MediaWorkerThread(object arg)
        //{
        //    // Create the VisualTargetPresentationSource and then signal the
        //    // calling thread, so that it can continue without waiting for us.
        //    var hostVisual = (HostVisual)arg;
        //    var visualTargetPS = new VisualTargetPresentationSource(hostVisual);
        //    s_event.Set();

        //    // Create a MediaElement and use it as the root visual for the
        //    // VisualTarget.
        //    visualTargetPS.RootVisual = CreateMediaElement();

        //    // Run a dispatcher for this worker thread.  This is the central
        //    // processing loop for WPF.
        //    System.Windows.Threading.Dispatcher.Run();
        //}

        //private FrameworkElement CreateMediaElement()
        //{
        //    waitIndicator = new ListBox
        //    {
        //        //ItemsSource = Output
        //    };
        //    waitIndicator.Background = Brushes.Pink;
        //    waitIndicator.UpdateLayout();

        //    return waitIndicator;
        //}






        [MediatorMessageSink("AppendOutputString", ParameterType = typeof(string))]
        public void AppendOutputString(string output)
        {
            //waitIndicator.Dispatcher.Invoke(new Action(delegate()
            //{
            //    Output.Insert(0, output);
            //}));

            this.Dispatcher.Invoke(new Action(delegate()
            {
                Output.Insert(0, output);
            }));
        }

        IList<string> _output;
        public IList<string> Output
        {
            get
            {
                if (_output == null)
                    _output = new ObservableCollection<string>();
                return _output;
            }
        }

        [MediatorMessageSink("DatabaseExetueFinished")]
        public void DatabaseExetueFinished()
        {
            this.Dispatcher.Invoke(DispatcherPriority.Render, new Action(delegate()
            {
                var dc = DataContext as WizardWindowViewModel;
                if (dc == null)
                    return;

                dc.IsExecutingScripts = false;

                // force the commandmanager to reevaluate
                CommandManager.InvalidateRequerySuggested();
            }));
        }
    }
}
